package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbMachineCabinet;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbMachineCabinetRepository extends JpaRepository<CmdbMachineCabinet, Integer>, JpaSpecificationExecutor<CmdbMachineCabinet> {
    @Query(value = "select m from CmdbMachineCabinet m where m.isDelete = 0 " +
            "and (:#{#paramSearchDto.machineCabinetCode} is null OR m.machineCabinetCode LIKE CONCAT('%', :#{#paramSearchDto.machineCabinetCode}, '%')) " +
            "and (:#{#paramSearchDto.uAmount} is null OR m.uAmount = :#{#paramSearchDto.uAmount}) " +
            "and (:#{#paramSearchDto.machineRoomId} is null OR m.machineRoomId = :#{#paramSearchDto.machineRoomId}) " +
            "and (:#{#paramSearchDto.cabinetSn} is null OR m.cabinetSn LIKE CONCAT('%', :#{#paramSearchDto.cabinetSn}, '%')) " +
            "and (:#{#paramSearchDto.aRoadPowerPortTotal} is null OR m.aRoadPowerPortTotal = :#{#paramSearchDto.aRoadPowerPortTotal}) " +
            "and (:#{#paramSearchDto.bRoadPowerPortTotal} is null OR m.bRoadPowerPortTotal = :#{#paramSearchDto.bRoadPowerPortTotal}) " +
            "and (:#{#paramSearchDto.isSts} is null OR m.isSts = :#{#paramSearchDto.isSts}) " +
            "and (:#{#paramSearchDto.eleLinePortTotal} is null OR m.eleLinePortTotal = :#{#paramSearchDto.eleLinePortTotal}) " +
            "and (:#{#paramSearchDto.aRoadPowerPortUsage} is null OR m.aRoadPowerPortUsage = :#{#paramSearchDto.aRoadPowerPortUsage}) " +
            "and (:#{#paramSearchDto.bRoadPowerPortUsage} is null OR m.bRoadPowerPortUsage = :#{#paramSearchDto.bRoadPowerPortUsage}) " +
            "and (:#{#paramSearchDto.eleLinePortUsage} is null OR m.eleLinePortUsage = :#{#paramSearchDto.eleLinePortUsage}) " +
            "and (:#{#paramSearchDto.type} is null OR m.type = :#{#paramSearchDto.type}) " +
            "and (:#{#paramSearchDto.lightLinePortTotal} is null OR m.lightLinePortTotal = :#{#paramSearchDto.lightLinePortTotal}) " +
            "and (:#{#paramSearchDto.lightLinePortUsage} is null OR m.lightLinePortUsage = :#{#paramSearchDto.lightLinePortUsage}) " +
            "and (:#{#paramSearchDto.uStartPosition} is null OR m.uStartPosition = :#{#paramSearchDto.uStartPosition}) " +
            "and (:#{#paramSearchDto.uEndPosition} is null OR m.uEndPosition = :#{#paramSearchDto.uEndPosition})")
    Page<CmdbMachineCabinet> searchByParam(Pageable pageable, CmdbMachineCabinet paramSearchDto);

    List<CmdbMachineCabinet> findAllByIdIn(List<Long> id);

//    Page<CmdbMachineCabinet> searchByParam(Pageable pageable, CmdbMachineCabinet paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_machine_cabinet set is_delete = :deleted where machine_cabinet_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);
}



